package com.jyx.user.controller;

import com.jyx.common.response.AppResponse;
import com.jyx.user.component.SmsTemplate;
import com.jyx.user.pojo.TMember;
import com.jyx.user.service.UserService;
import com.jyx.user.vo.req.UserRegisterVo;
import com.jyx.user.vo.resp.UserRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块,完成用户的注册,登录,获取验证码功能")
public class UserController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private UserService userService;

    @PostMapping("/sendSms")
    @ApiOperation("获取验证码")
    //获取验证码
    public AppResponse<Object> sendSms(String mobile){
        //创建验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        //存储验证码到redis(五分钟后消失)
        redisTemplate.opsForValue().set(mobile,code,5, TimeUnit.MINUTES);
        //发送短信
        String sendResponse = smsTemplate.sendSms(mobile, code);
        if (sendResponse == null && "fail".equals(sendResponse)){
            return AppResponse.fail("短信发送失败");
        }
        return AppResponse.ok(sendResponse);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public AppResponse<Object> register(UserRegisterVo member){
        //验证码验证
        //从redis中获取验证码
        String code = redisTemplate.opsForValue().get(member.getLoginacct());
        //验证码不为空
        if (!StringUtils.isEmpty(code)){
            //比较redis中的验证码和用户输入的验证码是否相同
            if (!member.getCode().equals(code)){
                return AppResponse.fail("验证码错误");
            }
            TMember tMember = new TMember();
            BeanUtils.copyProperties(member,tMember);
            try {
                userService.registerUser(tMember);
                return AppResponse.ok("注册成功");
            } catch (Exception e) {
                e.printStackTrace();
                return AppResponse.fail(e.getMessage());
            }
        }else {
            return AppResponse.fail("验证码已失效");
        }
    }

    @ApiOperation("用户登录功能")
    @PostMapping("/login")
    public AppResponse<UserRespVo> login(String loginacct, String userpswd){
        //判断用户是否存在
        TMember member = userService.login(loginacct, userpswd);
        if (member == null){//登录失败
            AppResponse<UserRespVo> response = new AppResponse<>();
            response.setMsg("登录失败");
            return response;
        }
        //登录成功
        //创建令牌
        String token = UUID.randomUUID().toString().replace("-", "");
        //创建返回对象
        UserRespVo userRespVo = new UserRespVo();
        //添加令牌
        userRespVo.setAccessToken(token);
        //将meber中的数据拷贝到userRespVo
        BeanUtils.copyProperties(member,userRespVo);
        //在redis中存储登录数据  键:令牌,值:用户:id
        redisTemplate.opsForValue().set(token,member.getId().toString(),24,TimeUnit.HOURS);
        return AppResponse.ok(userRespVo);
    }

    @ApiOperation("根据id获取用户信息")
    @GetMapping("/getUser/{id}")
    public AppResponse<UserRespVo> getUserById(@PathVariable("id")Integer id){
        TMember member = userService.findTMemberById(id);
        UserRespVo userRespVo = new UserRespVo();
        BeanUtils.copyProperties(member,userRespVo);
        return AppResponse.ok(userRespVo);
    }

}
